package com.mmlc.meimei.lichengkongjian.customview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.mmlc.meimei.lichengkongjian.R;


public class AutoSlideLayout extends FrameLayout {
    public boolean leftShow;
    public int right_height;
    public int right_widht;
    public boolean isResetRightViewWidth = true;

    public AutoSlideLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                getViewTreeObserver().removeOnPreDrawListener(this);

                initView();

                return false;
            }
        });
    }

    private void initView() {

        leftView = findViewById(R.id.left);

        rightView = findViewById(R.id.right);

        int width;
        if (isResetRightViewWidth) {
            WindowManager wm = (WindowManager) getContext()
                    .getSystemService(Context.WINDOW_SERVICE);
            width = (int) (wm.getDefaultDisplay().getWidth() * 0.8);

            ViewGroup.LayoutParams layoutParams = rightView.getLayoutParams();
            layoutParams.width = width;
            rightView.setLayoutParams(layoutParams);
        } else {
            width = rightView.getMeasuredWidth();
        }

        right_widht = width;
        right_height = rightView.getMeasuredHeight();

        leftView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftShow) {
                    startAnima(false);
                } else {
                    startAnima(true);
                }
            }
        });
        rightView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftShow) {
                    startAnima(false);
                } else {
                    startAnima(true);
                }
            }
        });
    }

    public View leftView;
    public View rightView;
    public float startX;
    public float endX;

    public void startAnima(boolean isOpen) {
        leftView.setClickable(false);
        rightView.setClickable(false);

        if (isOpen) {//打开动画
            startX = 0;
            endX = -right_widht;

        } else {//关闭动画
            startX = -right_widht;
            endX = 0;
        }

        execAnimator(leftView, startX, endX, isOpen);
        execAnimator(rightView, startX, endX, isOpen);

    }

    private void execAnimator(View view, float startX, float endX, final boolean isOpen) {
        ObjectAnimator openAima1 = ObjectAnimator.ofFloat(view, "translationX", startX, endX);
        openAima1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (isOpen) {
                    leftShow = true;
                } else {
                    leftShow = false;
                }

                leftView.setClickable(true);
                rightView.setClickable(true);
            }
        });
        openAima1.setDuration(500);
        openAima1.start();
    }
}
